#include "./app_uart.h"
#include "./app_flash.h"
#include "./app_alarm.h"
#include "./app_adc.h"
#include "./app_dac.h"
#include "./app_can.h"
uint8_t uart_rx_buf[64];
uint8_t uart_tx_buf[64];
struct uart_dev_t *uart1_dev;
struct vol_fator_t setparam;
extern uint16_t ioutMaxVal,voutMaxVal,voutMinVal,vinMaxVal,vinMinVal,tempMaxVal,fanMaxVal;
uint16_t voutMaxDefault = 6000;

extern uint32_t dacSetSeed ;

void app_uart1_init(void)
{
    uart_init(&uart1_dev);
}

uint8_t st_flag = 0;

uint8_t uart_count = 0;
uint8_t uart_rx_flag = 0;
static union ufloat_cons_t ufloat_cons;
static void app_uart_update_1(void)
{

    uart_tx_buf[0]=0xee;
    uart_tx_buf[1]=0xee;
    uart_tx_buf[2]=UART_DEV_TO_HOST_BACK_INFO;//设备信息
    uart_tx_buf[3]=0;
    ufloat_cons.fval=VolTemp;
    uart_tx_buf[4]=ufloat_cons.farray[0];
    uart_tx_buf[5]=ufloat_cons.farray[1];
    uart_tx_buf[6]=ufloat_cons.farray[2];
    uart_tx_buf[7]=ufloat_cons.farray[3];
    ufloat_cons.fval=VolVin ;
    uart_tx_buf[8]=ufloat_cons.farray[0];
    uart_tx_buf[9]=ufloat_cons.farray[1];
    uart_tx_buf[10]=ufloat_cons.farray[2];
    uart_tx_buf[11]=ufloat_cons.farray[3];
    ufloat_cons.fval=VolVout ;
    uart_tx_buf[12]=ufloat_cons.farray[0];
    uart_tx_buf[13]=ufloat_cons.farray[1];
    uart_tx_buf[14]=ufloat_cons.farray[2];
    uart_tx_buf[15]=ufloat_cons.farray[3];
    ufloat_cons.fval=VolIout    ; 
    uart_tx_buf[16]=ufloat_cons.farray[0];
    uart_tx_buf[17]=ufloat_cons.farray[1];
    uart_tx_buf[18]=ufloat_cons.farray[2];
    uart_tx_buf[19]=ufloat_cons.farray[3];
    ufloat_cons.fval=VolPerCharge;
    uart_tx_buf[20]=ufloat_cons.farray[0];
    uart_tx_buf[21]=ufloat_cons.farray[1];
    uart_tx_buf[22]=ufloat_cons.farray[2];
    uart_tx_buf[23]=ufloat_cons.farray[3];
     uart_tx_buf[24]=warn_device->fault_flag_buf[0];
    uart_tx_buf[25]=0xaa;
    uart1_dev->send_buffer(uart1_dev,uart_tx_buf,26);

}
static void app_uart_update_2(void)
{
    uint16_t val = 0;
    uart_tx_buf[0]=0xee;
    uart_tx_buf[1]=0xee;
    uart_tx_buf[2]=UART_DEV_TO_HOST_BACK_INFO_1;//设备信息
    uart_tx_buf[3]=0;//返回采样值
    uart_tx_buf[4]=analog_device->fact_vol[0];
    uart_tx_buf[5]=analog_device->fact_vol[0]>>8;

    uart_tx_buf[6]=analog_device->fact_vol[1];
    uart_tx_buf[7]=analog_device->fact_vol[1]>>8;

    uart_tx_buf[8]=analog_device->fact_vol[2];
    uart_tx_buf[9]=analog_device->fact_vol[2]>>8;

    uart_tx_buf[10]=analog_device->fact_vol[3];
    uart_tx_buf[11]=analog_device->fact_vol[3]>>8;
    
    uart_tx_buf[12]=analog_device->fact_vol[4];
    uart_tx_buf[13]=analog_device->fact_vol[4]>>8;
    val = output_dac->y_setting ;
    uart_tx_buf[14]=val;
    uart_tx_buf[15]=val>>8;
    uart_tx_buf[16]=warn_device->fault_flag_buf[0];
    uart_tx_buf[17]=0xaa;
    uart1_dev->send_buffer(uart1_dev,uart_tx_buf,18);

}
static void uart_tx_setup_info(void)
{
    int i;
    uart_tx_buf[0]=0xee;
    uart_tx_buf[1]=0xee;
    uart_tx_buf[2]=UART_DEV_TO_HOST_BACK_FACTOR_INFO;//设备信息
    for (i=0;i<4;i++)
    {
        uart_tx_buf[3+i*6] = analog_device->factor[i+1].in_setting;
        uart_tx_buf[4+i*6] = analog_device->factor[i+1].in_setting>>8;
        uart_tx_buf[5+i*6] = analog_device->factor[i+1].max_dis_value;
        uart_tx_buf[6+i*6] = analog_device->factor[i+1].max_dis_value>>8;
        uart_tx_buf[7+i*6] = analog_device->factor[i+1].in_sample;
        uart_tx_buf[8+i*6] = analog_device->factor[i+1].in_sample>>8;
    }
    uart_tx_buf[27]=output_dac->factor->k_val;
    uart_tx_buf[28]=output_dac->factor->k_val>>8;

    uart_tx_buf[29]=output_dac->factor->u_vout;
    uart_tx_buf[30]=output_dac->factor->u_vout>>8;

    uart_tx_buf[31]=output_dac->factor->v_default;
    uart_tx_buf[32]=output_dac->factor->v_default>>8;

    
    uart_tx_buf[33]=comm_dev->obj.master_addr;
    uart_tx_buf[34]=0xaa;
    uart1_dev->send_buffer(uart1_dev,uart_tx_buf,35);
    
}
static void uart_tx_threshold_info(void)
{
    uart_tx_buf[0] =0xee;
    uart_tx_buf[1] =0xee;
    uart_tx_buf[2] =UART_DEV_TO_HOST_BACK_INFO_THRESHOD;//设备信息
	  uart_tx_buf[3] =voutMaxVal;
    uart_tx_buf[4] =voutMaxVal>>8; 
	  uart_tx_buf[5] =voutMinVal;
    uart_tx_buf[6] =voutMinVal>>8;
    uart_tx_buf[7] =ioutMaxVal;
    uart_tx_buf[8] =ioutMaxVal>>8;
    uart_tx_buf[9] =vinMaxVal;
    uart_tx_buf[10]=vinMaxVal>>8;
    uart_tx_buf[11]=vinMinVal;
    uart_tx_buf[12]=vinMinVal>>8;
    uart_tx_buf[13]=tempMaxVal;
    uart_tx_buf[14]=tempMaxVal>>8;
    uart_tx_buf[15]=fanMaxVal;
    uart_tx_buf[16]=fanMaxVal>>8;
    uart_tx_buf[17]=0xaa;
    uart1_dev->send_buffer(uart1_dev,uart_tx_buf,18);	
}
/**
 *  协议说明：整帧数据长度 8个字节
 *            帧头 0xee ,0xee
 *            帧尾 0xaa
 * ee ee 05 02 03 04 05 aa
*/
void app_uart1_rx_byte(uint8_t data)
{
    if(uart_rx_flag==1)
    {
        if(data==0xee)
        {
            uart_rx_flag=2;
        }
    }
   if((data==0xee)&&(uart_rx_flag==0))
   {
       uart_rx_flag = 1;
		   uart_rx_buf[0] = data;
   }
   if(uart_rx_flag==2)
   {
		   uart_rx_buf[uart_count+1]=data;
		   uart_count++;
   }
   if(uart_count==0x9)
   {
       if(uart_rx_buf[uart_count]==0xaa)
       {
             uart_rx_flag=3;
       }
       else{
            uart_rx_flag = 0;
			uart_count=0; 
       }
   }
   
}
/*
* @本机can地址
 */
uint32_t canSlaveAddr;
/*
* @上传can 地址
 */
void uart_tx_upload_can_addr(void)
{
	  uart_tx_buf[0] =0xee;
    uart_tx_buf[1] =0xee;
    uart_tx_buf[2] =UART_DEV_TO_HOST_BACK_CAN_ADDR;//设备信息
	  uart_tx_buf[3] =canSlaveAddr;
    uart_tx_buf[4] =canSlaveAddr>>8; 
	  uart_tx_buf[5] =canSlaveAddr>>16;
    uart_tx_buf[6] =canSlaveAddr>>24;
    uart_tx_buf[7]=0xaa;
    uart1_dev->send_buffer(uart1_dev,uart_tx_buf,8);	
}

void app_uart1_parse(void)
{
   switch(uart_rx_buf[2])
    {
        case UART_HOST_TO_DEV_GET_INFO               :
            app_uart_update_1();
        break;
        case UART_HOST_TO_DEV_GET_INFO_1 :
            app_uart_update_2();
        break;
        case UART_HOST_TO_DEV_GET_FACTOR_INFO    ://
            uart_tx_setup_info();
        break;
				case UART_HOST_TO_DEV_GET_THRESHOD_INFO:
						uart_tx_threshold_info();
				break;
				case UART_HOST_TO_DEV_GET_CAN_ADDR:
						uart_tx_upload_can_addr();
				break;
				case UART_HOST_TO_DEV_SET_CAN_ADDR://设置can地址
						canSlaveAddr=uart_rx_buf[3]|(uart_rx_buf[4]<<8)|(uart_rx_buf[5]<<16);
						app_store_can_addr_default(canSlaveAddr);
				break;
        case UART_HOST_TO_DEV_SET_FACTOR_TEMP     ://
        setparam.in_setting = uart_rx_buf[3]|uart_rx_buf[4]<<8;
        setparam.in_sample = analog_device->get_voltage(analog_device,ANALOG_FACTOR_NUM_TEMP);//获取当前真实采样值
        setparam.max_dis_value = uart_rx_buf[5]|uart_rx_buf[6]<<8;;
        analog_device->set_factor(analog_device,ANALOG_FACTOR_NUM_TEMP     ,&setparam);
        //保存到flash中
        app_store_adc_fator(ANALOG_FACTOR_NUM_TEMP,&setparam);
        break;
        case UART_HOST_TO_DEV_SET_FACTOR_VIN      ://
            setparam.in_setting = uart_rx_buf[3]|uart_rx_buf[4]<<8;
            setparam.in_sample = analog_device->get_voltage(analog_device,ANALOG_FACTOR_NUM_VIN);//获取当前真实采样值
            setparam.max_dis_value = uart_rx_buf[5]|uart_rx_buf[6]<<8;;
            analog_device->set_factor(analog_device,ANALOG_FACTOR_NUM_VIN      ,&setparam);
            //保存到flash中
            app_store_adc_fator(ANALOG_FACTOR_NUM_VIN,&setparam);
        break;
        case UART_HOST_TO_DEV_SET_FACTOR_VOUT     ://
            setparam.in_setting = uart_rx_buf[3]|uart_rx_buf[4]<<8;
            setparam.in_sample = analog_device->get_voltage(analog_device,ANALOG_FACTOR_NUM_VOUT);//获取当前真实采样值
            setparam.max_dis_value = uart_rx_buf[5]|uart_rx_buf[6]<<8;;
            analog_device->set_factor(analog_device,ANALOG_FACTOR_NUM_VOUT     ,&setparam);
            //保存到flash中
						app_store_adc_fator(ANALOG_FACTOR_NUM_VOUT,&setparam);
        break;
        case UART_HOST_TO_DEV_SET_FACTOR_IOUT://
            setparam.in_setting = uart_rx_buf[3]|uart_rx_buf[4]<<8;
            setparam.in_sample = analog_device->get_voltage(analog_device,ANALOG_FACTOR_NUM_IOUT);//获取当前真实采样值
            setparam.max_dis_value = uart_rx_buf[5]|uart_rx_buf[6]<<8;;
            analog_device->set_factor(analog_device,ANALOG_FACTOR_NUM_IOUT     ,&setparam);
            //保存到flash中
			  app_store_adc_fator(ANALOG_FACTOR_NUM_IOUT,&setparam);
        break;
        case UART_HOST_TO_DEV_SET_FACTOR_PRECHARGE://
            setparam.in_setting = uart_rx_buf[3]|uart_rx_buf[4]<<8;
            setparam.in_sample = analog_device->get_voltage(analog_device,ANALOG_FACTOR_NUM_PRECHARGE);//获取当前真实采样值
            setparam.max_dis_value = uart_rx_buf[5]|uart_rx_buf[6]<<8;;
           analog_device->set_factor(analog_device,ANALOG_FACTOR_NUM_PRECHARGE,&setparam);
            //保存到flash中
			  app_store_adc_fator(ANALOG_FACTOR_NUM_PRECHARGE,&setparam);
        break;
        case UART_HOST_TO_DEV_SET_DAC_FACTOR://
        output_dac->factor->k_val = uart_rx_buf[3]|uart_rx_buf[4]<<8;
        output_dac->factor->u_vout = uart_rx_buf[5]|uart_rx_buf[6]<<8;
        output_dac->factor->v_default = uart_rx_buf[7]|uart_rx_buf[8]<<8;
        app_store_dac_fator(output_dac->factor);
				break;
        case UART_HOST_TO_DEV_SET_DAC_Y://
        output_dac->change_setting_val(output_dac,(uart_rx_buf[3]|uart_rx_buf[4]<<8));
        break;
      //  case UART_HOST_TO_DEV_SET_CAN_ADDR://
       // comm_dev->set_src_id(comm_dev,uart_rx_buf[3]);
      //  app_store_can_addr(uart_rx_buf[3]);
      //  break;
        case UART_HOST_TO_DEV_SET_VOUT_MAX://
					voutMaxVal = (uart_rx_buf[3]|uart_rx_buf[4]<<8);
					app_store_vout_max(voutMaxVal);
        break;
        case UART_HOST_TO_DEV_SET_IOUT_MAX://
					ioutMaxVal = (uart_rx_buf[3]|uart_rx_buf[4]<<8);
					app_store_iout_max(ioutMaxVal);
        break;
        case  UART_HOST_TO_DEV_SET_DAC_Default://默认输出值电压值
					
				dacSetSeed = 20;
        voutMaxDefault= uart_rx_buf[3]|uart_rx_buf[4]<<8;
					app_store_vout_max_default(voutMaxDefault);
        break;
				case UART_HOST_TO_DEV_SET_VOUT_MIN :
        voutMinVal= uart_rx_buf[3]|uart_rx_buf[4]<<8;
					app_store_vout_min(voutMinVal);
				break;
				case UART_HOST_TO_DEV_SET_VIN_MAX  :
        vinMaxVal= uart_rx_buf[3]|uart_rx_buf[4]<<8;
					app_store_vin_max(vinMaxVal);
				break;
				case UART_HOST_TO_DEV_SET_VIN_MIN  :
        vinMinVal= uart_rx_buf[3]|uart_rx_buf[4]<<8;
						app_store_vin_min(vinMinVal);
				break;
				case UART_HOST_TO_DEV_SET_FAN_MAX  :
        fanMaxVal= uart_rx_buf[3]|uart_rx_buf[4]<<8;
					app_store_fan_max(fanMaxVal);
				break;
				case UART_HOST_TO_DEV_SET_TEMP_MAX :
        tempMaxVal= uart_rx_buf[3]|uart_rx_buf[4]<<8;
					app_store_temp_max(tempMaxVal);
				break;
    }
}
void app_uart1_loop_func(void)
{
    if(uart_rx_flag==3)
    {
        app_uart1_parse();
        uart_rx_flag = 0;
		uart_count=0;
    }

}

